package com.dynamic.mybatis.core.parser.field;

import com.dynamic.mybatis.core.enums.CommandType;
import com.dynamic.mybatis.core.metadata.DynamicMappedStatement;
import com.dynamic.mybatis.core.metadata.MappedStatementMetaField;
import com.dynamic.mybatis.core.mock.Mock;
import com.dynamic.mybatis.core.parser.NamespaceHelper;
import com.dynamic.mybatis.core.session.DynamicSqlSession;
import com.dynamic.mybatis.core.utils.BeanUtils;
import com.dynamic.mybatis.core.utils.CollectionUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.dom4j.DocumentException;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author xs
 * @Date 2022/11/17 11:05
 */
public class DynamicMixedMappedStatementFieldParser extends DynamicMappedStatementFieldFormatter {


    public DynamicMixedMappedStatementFieldParser(DynamicSqlSession dynamicSqlSession) {
        super(dynamicSqlSession);
    }

    @Override
    public Configuration getConfiguration() {
        return this.configuration;
    }

    /**
     * 　* @Description: 获取动态SQL字段信息
     */
    public DynamicMappedStatement parseMappedStatementField(DynamicMappedStatement dms){
        if (dms == null) {
            return dms;
        }
        //临时namespace
        MappedStatement mappedStatement = getConfiguration().getMappedStatement(dms.getId());
        //外部加载真实namespace
        if (mappedStatement == null) {
            mappedStatement = getConfiguration().getMappedStatement(dms.getId());
        }
        //组装结果字段信息
        DynamicMappedStatement.Builder dmsBuilder = dms.toBuilder();
        if (dms.getCommandType() == CommandType.StandardCommand.SELECT) {
            //获取数据库字段信息
            List<MappedStatementMetaField> databaseFields = getDataBaseFieldQueryMappedStatement(dynamicSqlSession, dms, mappedStatement, null);
            byte[] serializeFields = BeanUtils.serialize(databaseFields);

            //设置相关表名
            Collection<String> tables = databaseFields.stream().map(f -> f.getTableName()).collect(Collectors.toSet());
            dms.setTables(tables);

            //组装请求字段信息
            MappedStatementMetaField inputParameterFields = inputParameterFieldParser(BeanUtils.deserialize(serializeFields), dms, true);
            dmsBuilder.setInputParameter(inputParameterFields).setInputParameterMock(Mock.mock(dms.getInputParameter().get(0)));


            MappedStatementMetaField outputParameterFields = outputParameterFieldParser(BeanUtils.deserialize(serializeFields), dms, mappedStatement);
            dmsBuilder.setOutputParameter(outputParameterFields);

            return dms;
        } else {
            List<MappedStatementMetaField> databaseFields = getDataBaseFieldUpdateMappedStatementField(dynamicSqlSession, dms, mappedStatement, dms.getInputParameterMock());

            MappedStatementMetaField parameterFields = inputParameterFieldParser(databaseFields, dms, false);
            Object parameter = Mock.mock(parameterFields);
            if (dms.isBatch() && parameter instanceof Collection) {
                parameter = ((Collection) parameter).iterator().next();
            }
            //数据库字段
            databaseFields = getDataBaseFieldUpdateMappedStatementField(dynamicSqlSession, dms, mappedStatement, parameter);

            //请求参数
            parameterFields = inputParameterFieldParser(databaseFields, dms, true);
            dmsBuilder.setInputParameter(parameterFields).setInputParameterMock(Mock.mock(dms.getInputParameter().get(0)));

            //响应参数
            MappedStatementMetaField resultFields = outputParameterFieldParser(dms);
            dmsBuilder.setOutputParameter(resultFields);

            return dms;
        }
    }

}